<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-10-5
 * Time: 上午10:30
 */

namespace App\Repositories;

use App\Exceptions\MyException;
class BaseRepository
{
    /**
     * 当前默认模型名
     * @var
     */
    protected $model;

    /**
     * 每页显示条数
     * @var
     */
    protected $pageSize;

    /**
     * BaseRepository constructor.
     */
    public function __construct()
    {
        $this->pageSize = config('admin.systems.page_size', 20);
    }

    /**
     * 获取模型主键
     * @return string
     */
    protected function getPrimaryKey()
    {
        return 'id';
    }

    /**
     * 根据主键编号获取单条数据
     * @param $id
     * @return mixed
     */
    public function getDetailById($id)
    {
        if (empty($id)) {
            return $this->model;
        }

        return $this->model->where($this->getPrimaryKey(), '=', $id)->first();
    }

    /**
     * 根据主键编号删除
     * @param array $ids
     * @return mixed
     * @throws MyException
     */
    public function deleteByIds(array $ids = [])
    {
        if (empty($ids)) {
            throw new MyException("删除的数据不能为空");
        }
        return $this->model->whereIn($this->getPrimaryKey(), $ids)->delete();
    }

    /**
     * 添加
     * @param array $info
     * @param null $model
     * @return null
     * @throws MyException
     */
    protected function add(array $info, $model = null)
    {
        if ($model === null) {
            $model = $this->model;
        }
        foreach ($info as $key=>$value) {
            if (empty($value) && $key == $this->getPrimaryKey()) {
                continue;
            }

            if ($value === '' || $value === null) {
                continue;
            }

            $model->$key = $value;
        }
        if (!$model->save()) {
            throw new MyException('添加信息失败');
        }

        return $model;
    }

    /**
     * 更新
     * @param array $info
     * @return mixed
     * @throws MyException
     */
    protected function edit(array $info, $model = null)
    {
        if ($model === null) {
            $model = $this->model;
        }
        $model = $model->find(get_value($info, $this->getPrimaryKey(), 0));
        if (empty($model)) {
            $model = $this->model;
        }
        foreach ($info as $key=>$value) {
            if ($value === '' || $value === null) {
                continue;
            }
            $model->$key = $value;
        }
        if (!$model->save()) {
            throw new MyException('更新信息失败');
        }

        return $model;
    }

    /**
     * 保存信息
     * @param array $info
     * @param string $action
     * @throws MyException
     */
    public function save(array $info, $action = '')
    {
        if (!method_exists($this, $action)) {
            throw (new MyException("不存在该方法", -1));
        }

        return $this->$action($info);
    }

    /**
     * 统一站点编号的判断方法
     * @param $model
     * @param $filter
     * @return mixed
     */
    protected function getSiteIdModel($model, $filter = [])
    {
        //  站点编号
        if (array_key_exists('site_id', $filter)) {
            if ($filter['site_id'] === null) {
                $filter['site_id'] = get_admin_current_site_id();
            }
            $model = $model->where('site_id', '=', (int) $filter['site_id']);
        }

        return $model;
    }

}